﻿using DataTransfer.Core.Models;
using DataTransfer.Core.Services;
using Microsoft.AspNetCore.Mvc;
using MiniFox.Platform;
using MiniFox.Web.Filters;
using MiniFox.Web.Infrastructure;
using MiniFox.Platform.Applications.Web;
using MiniFox;

namespace DataTransfer.Core.Controllers
{
    /// <summary>
    /// 数据导出API
    /// </summary>
    [NoCache, ValidationFilter, Area(GlobalConstants.AREA_NAME)]
    public class ExportController : BinaryControllerBase
    {
        protected virtual ExportEngine CreateComponentByName(string name, string exporterName)
        {
            return this.Container.GetComponentByName<ExportEngine>(name, exporterName, HttpContext.GetAuthState());
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="name"></param>
        /// <param name="exporter"></param>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult Load([FromRoute(Name = GlobalKey.NAME)] string name, [FromQuery] string exporter, [FromBody] ExportRequest request)
        {
            var component = this.CreateComponentByName(name, exporter);
            var fileData = component.Execute(request);

            return this.Execute(fileData.Data, fileData.ContentType, fileData.FileName, request.DispositionType);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="name"></param>
        /// <param name="exporter"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult Load([FromRoute(Name = GlobalKey.NAME)] string name, [FromQuery] string exporter)
        {
            var component = this.CreateComponentByName(name, exporter);
            var request = new ExportRequest
            {
                FileName = exporter
            };
            var fileData = component.Execute(request);

            return this.Execute(fileData.Data, fileData.ContentType, fileData.FileName);
        }
    }
}